package thread.knn;

import thread.knn.bean.BankMarketing;
import thread.knn.bean.Sample;
import thread.knn.distanceAlgorithm.EuclideanDistanceCalculator;
import thread.knn.task.ParallelGroupKnnClassifier;

import java.util.Date;
import java.util.List;

/*
    测试类
 */
public class Test_Main {
    public static void main(String[] args) {


//        testEuclidDistance();
        testTask(args);
    }
    //测试加载器
    public static void testLoader(){
        BankMarketingLoader loader = new BankMarketingLoader();
        //路径问题 不能使用绝对路径
//        System.out.println(System.getProperty("user.dir"));//获取到项目路径 D:\table\java\gitee_thread
//        System.out.println(System.getProperty("user.home"));
        //获取resources目录下的文件路径
        String dataPath = Test_Main.class.getClassLoader().getResource("bank.data").getPath();
        List<BankMarketing> data =
                loader.load(dataPath.substring(1));
        String testPath = Test_Main.class.getClassLoader().getResource("bank.test").getPath();
        List<BankMarketing> test =
                loader.load(testPath.substring(1));
        System.out.println(data.size());//39129
        System.out.println(test.size());//2059
    }
    //测试欧几里得距离
    public static void testEuclidDistance(){
        EuclideanDistanceCalculator calculator = new EuclideanDistanceCalculator();
        Sample sample1 = new Student(20,1.7);
        Sample sample2 = new Student(21,1.8);
        double distance = calculator.calculate(sample1,sample2);
        System.out.println(distance);
    }
    //测试任务
    public static void testTask(String[] args){
        BankMarketingLoader loader = new BankMarketingLoader();
        String dataPath = Test_Main.class.getClassLoader().getResource("bank.data").getPath();
        List<BankMarketing> data =
                loader.load(dataPath.substring(1));
        String testPath = Test_Main.class.getClassLoader().getResource("bank.test").getPath();
        List<BankMarketing> test =
                loader.load(testPath.substring(1));
        System.out.println(data.size());//39129
        System.out.println(test.size());//2059
        int k = 10;
        if (args.length>0 && args!=null){
            k = Integer.parseInt(args[0]);
        }
        //定义两个变量存测试集，通过模型预测准确率
        int success = 0, mistack = 0;
        int numThread = Runtime.getRuntime().availableProcessors();
        System.out.println("线程数："+numThread);
        ParallelGroupKnnClassifier classifier = new ParallelGroupKnnClassifier(k,numThread,data,true);
        long start = 0,end = 0;
        start = System.currentTimeMillis();
        //循环测试集中的数据，调用模型进行测试
        for (BankMarketing sample:test){
            //预测的类别
            String tag = classifier.classify(sample);
            if (tag.equals(sample.getTag())){
                success++;
            }else {
                mistack++;
            }
//            break;
        }
        end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));//20988
        System.out.println("success:"+success+" mistack:"+mistack+"正确率："+(double)success/test.size());//success:1873 mistack:186正确率：0.9096648858669257
    }
}

class Student extends Sample {
    private int age;
    private double height;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Student() {
    }

    public Student(int age, double height) {
        this.age = age;
        this.height = height;
    }

    @Override
    public String getTag() {
        return null;
    }

    @Override
    public double[] getExample() {
        return new double[0];
    }
}
